package com.huitone.gddw.service.impl.worktable;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.huitone.gddw.common.ComConstant;
import com.huitone.gddw.dao.entity.VirtualGateLink;
import com.huitone.gddw.dao.worktable.paramconfigmanage.IVirtualGateLinkDao;
import com.huitone.gddw.model.support.Message;
import com.huitone.gddw.model.support.QueryPage;
import com.huitone.gddw.service.IParamtabRecService;
import com.huitone.gddw.service.IVirtualGateLinkService;
import com.huitone.gddw.service.dto.VirtualGateLinkDTO;
import com.huitone.gddw.utils.SysLoginUtil;

@Service
public class VirtualGateLinkService implements IVirtualGateLinkService {
	
	@Autowired
	private IVirtualGateLinkDao virtualGateLinkDao;
	
	@Autowired
	private IParamtabRecService paramtabRecService;

	@Override
	public QueryPage<VirtualGateLinkDTO> selectPage(QueryPage<VirtualGateLink> page) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("gateLinkId", page.getParams().get("gateLinkId"));
		map.put("virtualGateLinkId", page.getParams().get("virtualGateLinkId"));
		map.put("start", (page.getPageNo() - 1) * page.getPageSize() + 1);
		map.put("end", page.getPageNo() * page.getPageSize());
		List<VirtualGateLinkDTO> result = new ArrayList<>();
		int totalCount = virtualGateLinkDao.selectCountByGateLinkId(map);
		List<String> gateLinkIdList = virtualGateLinkDao.getGateLinkId(map);
		
		for (String gateLinkId : gateLinkIdList) {
			VirtualGateLinkDTO virtualGateLinkDTO = this.getVirtualGateLink(gateLinkId);
			result.add(virtualGateLinkDTO);
		}
		
		QueryPage<VirtualGateLinkDTO> queryPage = new QueryPage<VirtualGateLinkDTO>();
		queryPage.setResult(result);
		queryPage.setPageNo(page.getPageNo());
		queryPage.setPageSize(page.getPageSize());
		queryPage.setTotalCount(totalCount);
		return queryPage;
	}

	@Override
	public VirtualGateLinkDTO toUpdate(String gateLinkId) {
		
		List<VirtualGateLinkDTO> list = virtualGateLinkDao.selectByGateLinkId(gateLinkId);
		VirtualGateLinkDTO virtualGateLinkDTO = new VirtualGateLinkDTO();
		String vIds = "";
		String vNames = "";
		if (list.size()>0) {
			virtualGateLinkDTO.setGateLinkId(list.get(0).getGateLinkId());
			virtualGateLinkDTO.setGateLinkName(list.get(0).getGateLinkName());
			for(VirtualGateLinkDTO entity : list){
				vIds += entity.getVirtualGateLinkId() + ",";
				vNames += entity.getVirtualGateLinkName() + ",";
			}
			virtualGateLinkDTO.setVirtualGateLinkId(vIds.substring(0, vIds.length()-1));
			virtualGateLinkDTO.setVirtualGateLinkName(vNames.substring(0, vNames.length()-1));
		}
		return virtualGateLinkDTO;
	}

	@Override
	public Message save(VirtualGateLinkDTO entity) {
		if (entity.getGateLinkId().isEmpty()) {
			return new Message(false, "通道不能为空！");
		}
		List<VirtualGateLinkDTO> list = virtualGateLinkDao.selectByGateLinkId(entity.getGateLinkId());
		if (list.size() > 0) {
			return new Message(false, "该通道的虚拟通道信息已存在！");
		}else {
			this.insert(entity);
			this.updateParamRec("保存虚拟通道信息");
			return new Message(true, "保存成功！");
		}
	}

	@Override
	public Message update(VirtualGateLinkDTO entity) {
		Map<String, Object> params = new HashMap<String, Object>();
		
		if (null == entity.getGateLinkId() || entity.getGateLinkId().equals("")) {
			return new Message(false, "请选择通道！");
		}
		params.put("gateLinkId", entity.getGateLinkId());
		int count = virtualGateLinkDao.selectCountByGateLinkId(params);
		if (count > 0 && !entity.getGateLinkId().equals(entity.getOldGateLinkId())) {
			return new Message(false, "该通道已存在！");
		}
		this.delete(entity.getOldGateLinkId());
		this.insert(entity);
		this.updateParamRec("更新虚拟通道信息");
		return new Message(true, "修改成功！");
	}

	@Override
	public void delete(String gateLinkId) {
		virtualGateLinkDao.deleteByGateLinkId(gateLinkId);
		this.updateParamRec("删除虚拟通道信息");		
	}
	
	/**
	 * 更新参数记录表
	 * @param desc 说明
	 */
	private void updateParamRec(String desc) {
		paramtabRecService.update(ComConstant.VARTUAL_GATE_LINK, desc);
	}

	@Override
	public VirtualGateLink selectByPrimaryKey(String recId) {
		return virtualGateLinkDao.selectByPrimaryKey(recId);
	}

	@Override
	public VirtualGateLinkDTO getVirtualGateLink(String gateLinkId) {
		VirtualGateLinkDTO virtualGateLinkDTO = new VirtualGateLinkDTO();
		virtualGateLinkDTO.setGateLinkId(gateLinkId);
		List<VirtualGateLinkDTO> list = virtualGateLinkDao.selectByGateLinkId(gateLinkId);
		if (list.size() > 0) {
			virtualGateLinkDTO.setGateLinkName(list.get(0).getGateLinkName());
			String vIds="";
			String vNames="";
			for(VirtualGateLinkDTO currentDTO : list){
				vIds += currentDTO.getVirtualGateLinkId() + ",";
				vNames += currentDTO.getVirtualGateLinkName() + ",";
			}
			virtualGateLinkDTO.setVirtualGateLinkId(vIds.substring(0, vIds.length()-1));
			virtualGateLinkDTO.setVirtualGateLinkName(vNames.substring(0, vNames.length()-1));
		}
		return virtualGateLinkDTO;
	}
	
	public void insert(VirtualGateLinkDTO entity){
		String gateLinkId = entity.getGateLinkId();
		String[] vIds = entity.getVirtualGateLinkId().split(",");
		for(String vId : vIds){
			VirtualGateLink virtualGateLink = new VirtualGateLink();
			virtualGateLink.setGateLinkId(gateLinkId);
			virtualGateLink.setVirtualGateLinkId(vId);
			virtualGateLink.setInsertTime(new Date());
			virtualGateLink.setInsertStaffId(SysLoginUtil.getLoginUserId());
			virtualGateLink.setLastModTime(virtualGateLink.getInsertTime());
			virtualGateLink.setLastModStaffId(virtualGateLink.getInsertStaffId());
			virtualGateLink.setStatus("1");//默认有效
			virtualGateLinkDao.insertSelective(virtualGateLink);
		}
	}

}
